<%#
 Copyright 2020-2024 sirpdboy Wich <sirpdboy@qq.com>
 https://github.com/sirpdboy/netspeedtest
 Licensed to the public under the Apache License 2.0.
-%>

<%+cbi/valueheader%>

<script type="text/javascript">//<![CDATA[

function speedtestportrun(btn) {

	var sid='speedtestport'
	var opt={
    		base:"cbid.netspeedtest."+sid,
    		get:function(opt){
    			var id=this.base+'.'+opt;
    			var obj=document.getElementsByName(id)[0] || document.getElementsByClassName(id)[0] || document.getElementById(id)
    			if (obj){
    				return obj;
    			}else{
    				return null;
    			}
    		},
    		getlist:function(opt){
    			var id=this.base+'.'+opt;
    			var objs=document.getElementsByName(id) || document.getElementsByClassName(id);
    			var ret=[];
    			if (objs){
    				for (var i=0;i < objs.length;i++){
    					ret[i]=objs[i].value;
    				}
    			}else{
    				alert("<%:Fatal on get option,please help in debug%>:"+opt);
	    		}
	    		return ret;
    		}
    }

    const RUN_URL = '<%=luci.dispatcher.build_url("admin", "network", "netspeedtest","test_port")%>';
    const S_URL = '<%=luci.dispatcher.build_url("admin", "network", "netspeedtest","speedtestport")%>';

	var output=document.getElementById("speedtestport-status");

	btn.value='<%:Waiting (executing)...%>';
	btn.disabled=true;
    var sdomain=opt.get("sdomain").value;
    var sport=opt.get("sport").value;
	XHR.get('<%=luci.dispatcher.build_url("admin/network/netspeedtest/test_port")%>',{
            sdomain: sdomain,
            sport: sport
			},
				(x) =>{
				
					if(x && x.status == 200) {
						  output.innerHTML="<font style=\'color:green\'>"+"<%:Perform OK%>"+"</font>";

					}
					else{
						  output.innerHTML="<font style=\'color:green\'>"+"<%:Test failed%>"+"</font>";
					}
						  setTimeout(function(){
			                 	window.location = S_URL
			             },1000);
					
				btn.disabled=false;
				btn.value='<%:Click to execute%>';
				}
		);
        return false;
    }
		

//]]></script>

	   <label class="cbi-value-title"><%= translate("Test server port delay") %></label>
  	   <div class="cbi-value-field">
  	       <input type="button" class="btn cbi-button cbi-button-apply" value='<%:Click to execute%>' onclick="return speedtestportrun(this)" />
               <span id="speedtestport-status"></span>
 	   </div>
<%+cbi/valuefooter%>
